package com.jacobson.core.di.modules

import android.app.Application
import android.content.Context
import android.provider.Settings
import androidx.fragment.app.FragmentManager
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.jacobson.core.App
import com.jacobson.core.framework.configuration.GsonConfiguration
import com.jacobson.core.framework.lifecycle.ActivityLifecycleCallbacksImpl
import com.jacobson.core.framework.lifecycle.FragmentLifecycleImpl
import com.jacobson.core.framework.lifecycle.RxActivityLifecycleImpl
import com.jacobson.core.framework.repository.IRepositoryManager
import com.jacobson.core.framework.repository.RepositoryManager
import com.jacobson.core.widget.storage.GlobalStorage
import dagger.Binds
import dagger.Module
import dagger.Provides
import java.util.ArrayList
import javax.inject.Named
import javax.inject.Singleton

/**
 * ================================================
 * 提供一些框架必须的实例
 * @author Wucz on 2018/7/18 15:23
 * ================================================
 */
@Module
abstract class AppModule {
    /**
     * 提供默认的 ActivityLifecycle
     */
    @Binds
    @Named("ActivityLifecycle")
    abstract fun bindActivityLifecycle(impl: ActivityLifecycleCallbacksImpl): Application.ActivityLifecycleCallbacks

    @Binds
    @Named("RxActivityLifecycle")
    abstract fun bindRxActviityLifecycle(impl: RxActivityLifecycleImpl): Application.ActivityLifecycleCallbacks

    @Binds
    abstract fun bindFragmentLifecycle(impl: FragmentLifecycleImpl): FragmentManager.FragmentLifecycleCallbacks

    @Binds
    internal abstract fun bindRepositoryManager(repositoryManager: RepositoryManager): IRepositoryManager

    @Module
    companion object {

        /**
         * 提供Gson
         */
        @Singleton
        @Provides
        @JvmStatic
        fun provideGson(application: Application, configuration: GsonConfiguration?): Gson {
            GsonBuilder().run {
                configuration?.onBuildInterceptor?.onBuildIntercept(application, this)
                return create()
            }
        }

        @Singleton
        @Provides
        @JvmStatic
        fun provideApp(application: Application): App = application as App

        @Singleton
        @Provides
        @JvmStatic
        @Named("applicationContext")
        fun provideApplicationContext(application: Application): Context = application


        @Singleton
        @Provides
        @JvmStatic
        fun provideFragmentLifecycles(): MutableList<FragmentManager.FragmentLifecycleCallbacks> = ArrayList()
    }
}